package com.hnevc.mytrafficdemo.util.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.entity.Car;
import com.hnevc.mytrafficdemo.entity.RechargeRecord;
import com.hnevc.mytrafficdemo.util.db.RechargeRecordDBUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/12/20.
 */

public class RechargeDialog extends Dialog {
    private static final String TAG = "RechargeDialog";
    private String dialogName;
    private List<Car> carList;
    TextView mTvCarNumbers;
    EditText mEtRechargeAmount;
    Button mBtnOK;
    Button mBtnCancel;
    Context mContext;
    private String mCarNumbers="";

    public RechargeDialog(@NonNull Context context, String dialogName, List<Car> carList) {

        super(context);
        mContext =context;
        this.dialogName = dialogName;
        this.carList = carList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rechargedialog_layout);
        this.setTitle(this.dialogName);

        mTvCarNumbers = (TextView) findViewById(R.id.id_iv_cardNums);
        mBtnOK = (Button) findViewById(R.id.id_btn_recharge);
        mBtnCancel = (Button) findViewById(R.id.id_btn_cancel);
        mEtRechargeAmount = (EditText) findViewById(R.id.id_et_recharge_amount);


        for(int i=0;i<carList.size();i++){
            Car car = carList.get(i);
            if(car.isNeedCharge()){
                mCarNumbers += car.getCardNum()+" ";
            }
        }
        mTvCarNumbers.setText(mCarNumbers);
        initEvent();

    }

    private void initEvent() {
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RechargeDialog.this.dismiss();
            }
        });
        mBtnOK.setOnClickListener(new View.OnClickListener() {
            private boolean mResult=false;

            @Override
            public void onClick(View view) {
                for (int i = 0; i < carList.size(); i++) {
                    Car car = carList.get(i);
                    if (car.isNeedCharge()) {
                        RechargeRecord record = new RechargeRecord();
                        record.setCarNum(car.getCardNum());
                        record.setBalance(car.getCarBalance()
                                +Integer.parseInt(mEtRechargeAmount.getText().toString()));
                        record.setRechargeAmount(Integer.parseInt(mEtRechargeAmount.getText().toString()));
                        record.setRecharger("user1");
                        //日期转换为字符串
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        record.setRechargeDate(sdf.format(new Date(System.currentTimeMillis())));
                        RechargeRecordDBUtil dbUtil = new RechargeRecordDBUtil(mContext);
                        mResult = dbUtil.insert(record);

                    }
                }
                Toast.makeText(mContext, mResult?mCarNumbers+"充值成功":mCarNumbers+"添加失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
